package com.leyou.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.Mapper.BrandMapper;
import com.leyou.PageResult;
import item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<Brand> queryBrandByPageAndSort(
            Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 开启分页助手
        PageHelper.startPage(page, rows);

        Example example = new Example(Brand.class);

/*
        // -----------------------------------------------------------------------
        // 过滤
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().andLike("name", "%" + key + "%").orEqualTo(
                    "letter", key.toUpperCase());
        }

        if (StringUtils.isNotBlank(sortBy)) {
            // 排序
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        // 查询
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        // 返回结果
        return new PageResult<>(pageInfo.getTotal(), pageInfo);

        // -------------------------------------------------------------------
        */


        // --------------------------------------------------------------
        Example.Criteria criteria1 = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();

        if (StringUtils.isNotBlank(key)) {
            criteria1.andLike("name", "%" + key + "%");
            criteria2.andEqualTo("letter", key);
        }

        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC " : " ASC "));
        }

        example.or(criteria2);

        // 这个操作目的和以前的pageInfo一致, 就是把查询结构操作一个统计函数, 自动统计总页数, 以及总条目数
        Page<Brand> brandPage = (Page<Brand>) this.brandMapper.selectByExample(example);

        // 根据分页查询的结果封装分页的结果对象
        return new PageResult<>(brandPage.getTotal(), new Long(brandPage.getPages()), brandPage.getResult());

    }

    // 开启事务
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
        // 新增品牌信息
        this.brandMapper.insertSelective(brand);
        // 新增品牌分类中间表
        cids.forEach(cid -> this.brandMapper.insertCategoryBrand(cid, brand.getId()));
    }

    @Transactional
    public void uploadBrand(Brand brand, List<Long> cids) {
        //将商品信息更新改变
        this.brandMapper.updateByPrimaryKeySelective(brand);
        //删除关联表中的记录
        this.brandMapper.deleteBrandCategory(brand.getId());
        //把当前的品牌以及分类的关系重新添加
        cids.forEach(cid->this.brandMapper.insertCategoryBrand(cid,brand.getId()));
    }

    @Transactional
    public void deleteByBrandId(Long bid) {
        this.brandMapper.deleteByPrimaryKey(bid);
        this.brandMapper.deleteBrandCategory(bid);
    }

    public Brand queryBrandById(Long brandId) {
        return this.brandMapper.selectByPrimaryKey(brandId);
    }

    public List<Brand> queryBrandByCategory(Long cid) {
        return this.brandMapper.queryByCategoryId(cid);
    }
}
